package org.xuyuji.test.jettydemo.server;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.xuyuji.test.jettydemo.servlet.HelloServlet;

public class ServletServer {
	public void start(int port) throws Exception{
		Server server = new Server(port);
		
		//访问路径:http://localhost:port/apple/hello.do 访问HelloServlet
        ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletHandler.setContextPath("/apple");
        servletHandler.addServlet(new ServletHolder(new HelloServlet()), "/hello.do");

        //访问路径：http://localhost:port/banana 访问默认首页 web/index.html
        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath("/banana");
        contextHandler.setResourceBase("web");
        contextHandler.setHandler(new ResourceHandler());//不放ResourceHandler则setResourceBase无效
        
        //访问路径：http://localhost:8089/orange/ 访问默认首页 web/index.html
        //访问路径：http://localhost:8089/orange/hello 访问HelloServlet
        WebAppContext webappContext = new WebAppContext();  
        webappContext.setContextPath("/orange");  
        webappContext.setResourceBase("web");  
        webappContext.setDefaultsDescriptor("web/WEB-INF/web.xml");
        webappContext.setHandler(new ResourceHandler());
 
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(new Handler[] {servletHandler, contextHandler, webappContext});
 
        server.setHandler(contexts);
 
        server.start();
        server.join();
	}
}
